package com.example.edu.controller;


import com.alibaba.excel.util.StringUtils;

import com.example.edu.client.VodClient;
import com.example.edu.exceptionhandler.HeartEduException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import com.example.edu.entity.Video;
import com.example.edu.service.VideoService;
import com.example.utils.Result;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-07-02
 */
@RestController
@RequestMapping("/edu")
public class VideoController {
	
	@Autowired
	private VideoService videoService;

	@Autowired
	private VodClient vodClient;
	
	@GetMapping("video/{id}")
	public Result getVideoById(@PathVariable String id) {
		Video video = videoService.getById(id);
		
		return Result.ok().data("video", video);
	}
	
	@PostMapping("video")
	public Result saveVideo(@RequestBody Video video) {
		videoService.save(video);
		return Result.ok();
	}
	
	@PutMapping("video")
	public Result updateVideo(@RequestBody Video video) {
		videoService.updateById(video);
		return Result.ok();
	}
	
	@DeleteMapping("video/{id}")
	public Result deleteVideo(@PathVariable String id) {
		Video video = videoService.getById(id);
		String videoSourceId = video.getVideoSourceId();
		if(!StringUtils.isEmpty(videoSourceId)){
			Result result = vodClient.deleteVideo(videoSourceId);
			if(result.getCode()==20001){
				throw new HeartEduException(20001,"删除错误熔断降级");
			}
		};

		videoService.removeById(id);
		return Result.ok();
	}

}

